In this video we learn how to sort pen out serious so let's assume we have the H column of our Titanic
data set and we want to sort the age from young to old or from 0 to young and we can do this with the
sort values method so that's option number one we can sort a panda series by values but we can also
swap a panda serious by the index.
So let's go into our examples here and we import pandas and then we create a dictionary with the daily
sales that we have on Monday.
So one sense for Monday we have 10 sets.
Then we have Wednesday 25 cents and then we have Tuesday success.
So our dictionary has not really sorted.
So Wednesday comes before Tuesday then we have a first day 36 sales.
Then Friday to say is Saturday cereals layers because we do not work on Saturday and Sunday as we do
not work on Sundays as well.
We have also cereal sales but instead of having a zero we have just a month so we can do this and let's
see what we get here.
We get here and dictionary with um weekdays we'll use Paris and you can see here on Sunday it's still
the value none included so let's pass our dictionary to PD dot series and let's create a pan that serious.
So now you can see here our panel serious with on the left side our keys of our dictionary and in the
same saw that as we passed it.
So beginning with the day one and then they three they four five six seven.
So here again.
And we also have on the right side the values and you can see on Sunday we AFE and they end on missing
value.
So the key about num actually transforms and append us serious and the PAN US data frame to a missing
value and and and as we do not have here the right sequence in our index so starting from Monday but
then having Wednesday and then Tuesday we can sort our index with the site index method.
So now you can see we have our index sorted starting from 1 and then we have 2 3 4 5 6 and 7 and we
can also determine whether we want an ascending or descending order.
So by default here we have an ascending order starting with them the lowest index.
So here 1 so here is the salt index method we have a parameter ascending so let's press shift tap here
and have a look so here we have the ascending parameter and by default it is set to true.
So let's run again here.
So we have here the ascending order but we can change this to the descending order by setting the parameter
to force.
So now we have from the descending order starting from Sunday or from seven seven six five four three
two one.
All right.
So now let's change it back to ascending order.
All right.
So now we have changed to our that serious.
Say it's by sorting the index and let's print again our panel series.
And now you can see here that our change so sorting the index is not stored in memory so we haven't
actually changed our series here and what we did here actually we just created a copy of our pandas
series says and we uh sorted the index of the copy only.
But uh yeah what.
We want to change our original series and that's a solution for it.
So let's go inside this site and next method and check with Chef tab.
And here is the parameter in place and in place is by a defined set of files.
And if it's true it performs the operation in place.
So what in place is doing if it's set to fires all changes we made.
So for example sorting the index are performed only on a copy and the original serious has not changed.
And the fifth set of the in-place parameter to true then all changes also apply for the original series
so the changes in place and the in-place perimeter and pan US is a very important parameter and you
can see it in many methods for panda serious and also for panda data frames and the tabs you a lot to
understand if a change is actually made on a copy or on the origin panda series or data frame and depending
on what you actually want to do you can set the in place to true or false.
So let's explicitly here introduce the in-place parameter and by default is set of false
so now here we created a copy of states and decided to index.
And if we print our original sales it's still not in place of the change here.
But if we change the in-place parameter to true
and now you can see here we do not see any uh printed copy of the say so that's the first good indication
that we changed the original say it's the panda series and let's print here.
Oh pen serious.
And now we can see that.
Yeah.
That changes in place.
So here we saw that our index and we changed our original pen series say it's all right.
So now let's head over to a sorting value.
So so far we only saw that our pandas serious by the index and we can also sort our serious by the values
and we set in place to force.
And let's see what we get here so by default the place apparently does set to fall.
So I only want to make this explicitly here and we can see here on the right side.
We have sorted ourselves pandas serious by the values.
And then ascending order.
So starting from zero Zs on Saturday and ending with 36 sets on Thursday and the end value is at the
very end and also for the site various method.
There are some parameters so we have the ascending parameter.
We can determine whether we want to sort an ascending or descending manner so by default that the ascending
parameter is set to true.
But we can set it to false.
Let's check here.
So now we sort out our values in descending order starting with so the highest value 36 on Thursday.
And we can also determine here you can see that still our any end value is in the last position and
we can also determine the position of um and a values.
So here we have last but we can also change this for example to first.
So now here we have our end end value at the first position but in the end.
So it makes more sense to have it at the last position
and sorting ourselves from high to low makes makes sense so we keep ascending equal scores.
And once we are happy here we can set our in-place parameter to true
and in this way we save our changes actually in our original panel series.
So let's run here and that's print our series and we can see that the changes we make here are in place.
All right so now let's again create our dictionary and now this Monday Tuesday Wednesday and so on the
strings and we can pass also this dictionary to uh PD dot series and create a panel series.
Let's have a look here.
And now if we sought the index with the salt index method then let's see what we get here.
So then our index is actually sorted in an alphabet equal manner.
So Friday starts with an F and then we have Monday with M first day Tuesday and Wednesday.
So if we have a numerical index uh then the numerical index is sorted.
Yeah from high to low and this a string index that's sorted uh alphabetically and we can also change
here the order from ascending to descending.
So now it's starting with uh the Wednesday w all right so now we are finished with this video and the
next video will have a look at the methods and largest and smallest so hope to see you there by.